<template>
  <view>
    <view class="search-box">
      <uni-search-bar @confirm="search" :focus="true" v-model="searchValue" @input="input" :radius="100" cancelButton="none"></uni-search-bar>
    </view>
    <view class="search-result-list" v-if="searchResults.length != 0">
      <view class="search-result-list-item" v-for="item in searchResults" :key="item.goods_id" @click="gotoDetail(item.goods_id)">
        <view class="goods_name">{{item.goods_name}}</view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
    
    <!-- 搜索历史 -->
    <view class="history-box" v-else>
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="clear"></uni-icons>
      </view>
      <view class="history-list">
        <uni-tag :text="item" v-for="(item, index) in historyList" :key="index" @click="gotoGoodsList(item)"></uni-tag>
      </view>
    </view>

  </view>
</template>

<script>
  export default {
    data() {
      return {
        searchValue: '',
        // 延时器的 timerId
        timer: null,
        searchResults: [],
        historyList: uni.getStorageSync('historyList') || []
      };
    },
    methods: {
      input(e) {
        // 清除 timer 对应的延时器
        clearTimeout(this.timer)
        // 重新启动一个延时器，并把 timerId 赋值给 this.timer
        this.timer = setTimeout(() => {
          // 如果 500 毫秒内，没有触发新的输入事件，则为搜索关键词赋值
          this.searchValue = e
          // 根据关键字搜索
          this.getSearchResult()
        }, 500)
      },
      // 搜索
      async getSearchResult() {
        if (this.searchValue === '') {
          this.searchResults = []
          return
        }
        const {data: res} = await uni.$http.get('/api/public/v1/goods/qsearch', {query: this.searchValue})
        this.searchResults = res.message
        this.saveSearchHistory()
      },
      gotoDetail(goodsId) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + goodsId
        })
      },
      // 保存搜索记录到本地存储
      saveSearchHistory() {
        // 解决关键字前后顺序的问题;解决关键词重复的问题
        this.historyList = [...new Set([this.searchValue, ...this.historyList])].slice(0,10);
        uni.setStorageSync('historyList', this.historyList)
      },
      clear() {
        this.historyList = []
        uni.setStorageSync('historyList', this.historyList)
      },
      // 点击跳转到商品列表页面
      gotoGoodsList(query) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?query=' + query
        })
      }
    }
  }
</script>

<style lang="scss">
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }
  
  .search-result-list {
    padding: 0 10px;
    
    .search-result-list-item {
      display: flex;
      align-items: center;
      justify-content: space-between;
      font-size: 12px;
      padding: 12px 0;
      border-bottom: 1px solid #efefef;
      
      // 文本单行显示
      .goods_name{
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
  }
  
  .history-box {
    padding: 0 5px;
    .history-title {
      display: flex;
      align-items: center;
      justify-content: space-between;
      height: 40px;
      font-size: 13px;
      border-bottom: 1px solid #efefef;
    }
    .history-list {
      display: flex;
      flex-wrap: wrap;
      uni-tag {
        margin-top: 5px;
        margin-right: 5px;
      }
    }
  }
</style>